import logzero
from PyQt5.QtWidgets import (
    QGridLayout,
    QGroupBox,
    QDialog,
    QPushButton,
    QRadioButton,
    QVBoxLayout,
    QInputDialog,
    QLineEdit,
    QMessageBox,
)
from collections import namedtuple
from qt_custom_widget.qt_TimerMessageBox import TimerMessageBox

SelectResult = namedtuple("SelectResult", "deviceType userType")


class TypeSelector(QDialog):
    def __init__(self, parent, logger=logzero.logger):
        super().__init__(parent)
        self.deviceType = 0
        self.userType = 0
        self.result = None
        self.logger = logger
        self.initUI()

    def initUI(self):
        self.setWindowTitle("通讯后台配置")
        self.createDeviceTypeGB()
        self.createUserTypeGB()
        layout = QGridLayout(self)
        button = QPushButton("确定", self)
        button.clicked.connect(self.dealButtonClick)
        layout.addWidget(self.deviceTypeGB, 0, 0)
        layout.addWidget(self.userTypeGB, 0, 1)
        layout.addWidget(button, 1, 0, 1, 2)

    def createDeviceTypeGB(self):
        self.deviceTypeGB = QGroupBox("型号选择", self)
        layout = QVBoxLayout(self.deviceTypeGB)
        self.devideRadio_1 = QRadioButton("X-系列")
        self.devideRadio_1.setChecked(True)
        self.devideRadio_1.clicked[bool].connect(self.dealDeviceRadio_1_Clicked)
        layout.addWidget(self.devideRadio_1)
        self.devideRadio_2 = QRadioButton("B-系列")
        self.devideRadio_2.clicked[bool].connect(self.dealDeviceRadio_2_Clicked)
        layout.addWidget(self.devideRadio_2)

    def createUserTypeGB(self):
        self.userTypeGB = QGroupBox("用户选择", self)
        layout = QVBoxLayout(self.userTypeGB)
        self.userRadio_1 = QRadioButton("普通用户")
        self.userRadio_1.setChecked(True)
        self.userRadio_1.clicked[bool].connect(self.dealUserRadio_1_Clicked)
        layout.addWidget(self.userRadio_1)
        self.userRadio_2 = QRadioButton("生产调试")
        self.userRadio_2.clicked[bool].connect(self.dealUserRadio_2_Clicked)
        layout.addWidget(self.userRadio_2)

    def dealDeviceRadio_1_Clicked(self, clicked):
        if clicked:
            self.deviceType = 0  # spac800-X

    def dealDeviceRadio_2_Clicked(self, clicked):
        if clicked:
            self.deviceType = 1  # spac800-B

    def dealUserRadio_1_Clicked(self, clicked):
        if clicked:
            self.userType = 0

    def dealUserRadio_2_Clicked(self, clicked):
        if clicked:
            inputStr, okPressed = QInputDialog.getText(self, "请输入密码", "", QLineEdit.Password)
            self.logger.info("QInputDialog | {} | {}".format(inputStr, okPressed))
            if self.deviceType == 0 and inputStr == "7171":  # spac800-X
                self.userType = 1
            elif self.deviceType == 1 and inputStr == "7171":  # spac800-B
                self.userType = 1
            else:
                messageBox = TimerMessageBox(parent=self, timeout=1)
                messageBox.setIcon(QMessageBox.Warning)
                messageBox.setWindowTitle("警告")
                messageBox.setText("密码错误")
                messageBox.setStandardButtons(QMessageBox.NoButton)
                messageBox.exec_()
                self.userRadio_1.setChecked(True)

    def updateSeleceResult(self):
        self.result = SelectResult(self.deviceType, self.userType)

    def dealButtonClick(self):
        self.updateSeleceResult()
        self.accept()
